
#include <iostream>
#include <pthread.h>
#include <vector>
#include <unistd.h>

pthread_mutex_t mutex = PTHREAD_MUTEX_INITIALIZER; // 定义锁
pthread_cond_t cond = PTHREAD_COND_INITIALIZER;    // 定义条件变量

struct ThreadData
{
    std::string thread_name_;
    pthread_t thread_id_;
};

void *thread_func(void *args)
{
    pthread_detach(pthread_self());
    ThreadData *data = static_cast<ThreadData *>(args);
    while (1)
    {
        pthread_mutex_lock(&mutex);
        pthread_cond_wait(&cond, &mutex);
        std::cout << data->thread_name_ << " wake up" << std::endl;
        pthread_mutex_unlock(&mutex);

    }
    return nullptr;
}

int main()
{
    std::vector<ThreadData *> threads;
    for (int i = 0; i < 5; i++)
    {
        ThreadData *pdata = new ThreadData();
        pdata->thread_name_ = "Thread-" + std::to_string(i + 1);
        pthread_create(&pdata->thread_id_, NULL, thread_func, pdata);
        threads.push_back(pdata);
        usleep(500);
    }
    while(1)
    {
        pthread_cond_signal(&cond);
        sleep(1);
    }
    return 0;
}